﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using GameView.Comment;
using GameView.Services;
using GameView.Services.Dtos.SysLogDto;
using System.Collections.ObjectModel;
using System.Windows.Controls;

namespace GameView.ViewModels
{
    public partial class SysLogViewModel : ObservableObject
    {
        private readonly SysLogService _service;

        private readonly IMessenger _messenger;

        public SysLogViewModel(SysLogService service)
        {
            _service = service;
            LoadData().ConfigureAwait(false);
        }

        [ObservableProperty]
        private bool _isLoading = false;

        [ObservableProperty]
        private int _pageIndex = 1;

        [ObservableProperty]
        private int _pageSize = 50;

        [ObservableProperty]
        private int _totalCount = 0;

        [ObservableProperty]
        private int _totalPages = 1;

        [ObservableProperty]
        private bool _hasPrevPages = false;

        [ObservableProperty]
        private int _pageNumber = 0;

        [ObservableProperty]
        private bool _hasNextPages = true;

        [ObservableProperty]
        private ObservableCollection<SysLogOutDto> _rechargeRecords = [];

        [ObservableProperty]
        private SysLogQueryDto _queryDto = new();

        [RelayCommand]
        private async Task FirstPage()
        {
            PageIndex = 1;
            PageNumber = 1;
            await LoadFromApi();
        }

        [RelayCommand]
        private async Task PreviousPageAsync()
        {
            PageIndex--;
            PageNumber--;
            HasPrevPages = PageIndex > 1;
            await LoadFromApi();
        }

        [RelayCommand]
        private async Task NextPageAsync()
        {
            PageIndex++;
            PageNumber++;
            await LoadFromApi();
        }

        [RelayCommand]
        private void LastPage() => PageIndex = TotalPages;

        [RelayCommand]
        private async Task ChangePageSize(object size)
        {
            ComboBoxItem cb = (ComboBoxItem)size;
            if (cb.Content is string sizeStr && int.TryParse(sizeStr.Replace("条/页", ""), out var newSize))
            {
                PageSize = newSize;
                PageIndex = 1; // 重置到第一页
                await LoadFromApi();
            }
        }

        [RelayCommand]
        private async Task SearchAsync()
        {
            IsLoading = true;
            await Task.Delay(200);
            try
            {
                PageIndex = 1;
                await LoadFromApi();
            }
            catch (Exception ex)
            {
                GlobalMessage.Instance.ShowError($"数据加载错误：{ex.Message}");
            }
            finally
            { IsLoading = false; }
        }

        [RelayCommand]
        private async Task LoadData()
        {
            await LoadFromApi();
        }

        private async Task LoadFromApi()
        {
            IsLoading = true;
            await Task.Delay(200);
            try
            {
                QueryDto.PageIndex = PageIndex;
                QueryDto.PageSize = PageSize;

                var res = await _service.GetPageAsync(QueryDto);
                if (res.StatusCode != 200)
                {
                    GlobalMessage.Instance.ShowError(JSON.Serialize(res.Errors));
                    return;
                }
                PageSize = res.Data.PageSize;
                TotalPages = res.Data.TotalPages;
                TotalCount = res.Data.TotalCount;
                HasNextPages = res.Data.HasNextPages;
                HasPrevPages = res.Data.HasPrevPages;
                PageIndex = res.Data.PageIndex;
                RechargeRecords.Clear();
                if (res.Data.Items == null)
                    return;
                foreach (var rr in res.Data.Items)
                {
                    RechargeRecords.Add(rr);
                }
            }
            catch (Exception ex)
            {
                GlobalMessage.Instance.ShowError($"数据加载错误：{ex.Message}");
            }
            finally
            { IsLoading = false; }
        }
    }
}